﻿define([
    'require',
    'angular',
    'underscore'
], function (require, angular, _) {
    return  function ($scope,Resource,GillionMsg,$dataSourceManager,$http, $timeout,$location,$rootScope) {
        $(document).ready(function(){
            $("#each li").click(
                function() {
                    $(this).css("background","#2ca1f2").siblings().css("background","");
                });

        });
        angular.element('.time-selection li:first').trigger('click');
        $http({
            method: 'GET',
            url: "/grule-manager/projects/arr"
        }).success(function (resp) {
            $scope.projectsName=resp
        })
        $scope.reportParams={};
        var endTime = new Date(),
            beginTime = new Date();
        endTime.setDate(endTime.getDate());
        beginTime.setDate(beginTime.getDate()-1);
        beginTime = beginTime.toISOString();
        endTime=endTime.toISOString();
        if($location.search().projects&&$location.search().packages){
            $http({
                method: 'GET',
                url: "/grule-manager/projects/id",
                params:{
                    packageId:$location.search().packages,
                    projectId:$location.search().projects
                }
            }).success(function (resp) {
                $scope.reportParams = {
                    projectName:resp.data.projectName,
                    packageName:resp.data.packageName,
                    startTime : beginTime,
                    endTime:endTime,
                    projectsName:$scope.projectsName
                };
                $dataSourceManager.dataSources["reportSource"].doRequestData(1,$scope.reportParams);
            });
        }else if($location.search().projects&&!$location.search().packages){
            $http({
                method: 'GET',
                url: "/grule-manager/projects/id",
                params:{
                    projectId:$location.search().projects
                }
            }).success(function (resp) {
                $scope.reportParams = {
                    projectName:resp.data.projectName,
                    startTime : beginTime,
                    endTime:endTime,
                    projectsName:$scope.projectsName
                };
                $dataSourceManager.dataSources["reportSource"].doRequestData(1,$scope.reportParams);
            });
        }else {
            $http({
                method: 'GET',
                url: "/grule-manager/projects/arr"
            }).success(function (resp) {
                $scope.reportParams = {
                    projectsName:resp,
                    startTime : beginTime,
                    endTime:endTime
                };
                $dataSourceManager.dataSources["reportSource"].doRequestData(1,$scope.reportParams);
            });
        }
        $scope.search = function () {
            $scope.reportParams.projectsName=$scope.projectsName;
            $scope.params=$scope.reportParams;
            getDataRange($scope);
            if(!$location.search().projects){
                $scope.params.projectName =$scope.reportParams.projectName;
            }
            if($scope.params.projectName==""){
                $scope.params.projectName=undefined;
            }
            if($scope.params.packageName==""){
                $scope.params.packageName=undefined;
            }
            $dataSourceManager.dataSources["reportSource"].params = $scope.params;
            $dataSourceManager.dataSources["reportSource"].doRequestData();
        };
        $scope.$on("reportSource",function(context,dataSource){
            console.log(dataSource.records);
            var totalRecord  = dataSource.records;

        });
        function getDataRange($scope) {
            if ($scope.dateRange.rangeType === "ONE_DAY") {
                var endTime = new Date(),
                    beginTime = new Date();
                endTime.setDate(endTime.getDate());
                beginTime.setDate(beginTime.getDate()-1);
                beginTime = beginTime.toISOString();
                $scope.params.startTime = beginTime;
                $scope.params.endTime = endTime.toISOString();
            } else if ($scope.dateRange.rangeType === "ONE_WEEK") {
                var endTime = (new Date()).toISOString(),
                    beginTime = new Date();
                beginTime.setDate(beginTime.getDate() - 7);
                beginTime = beginTime.toISOString();
                $scope.params.startTime = beginTime;
                $scope.params.endTime = endTime;
            } else if ($scope.dateRange.rangeType === "ONE_MONTH") {
                var endTime = (new Date()).toISOString(),
                    beginTime = new Date();
                beginTime.setMonth(beginTime.getMonth() - 1);
                beginTime = beginTime.toISOString();
                $scope.params.startTime = beginTime;
                $scope.params.endTime = endTime;
            }
        }

        $scope.showErrLog = function(row){
            var  params="?name="+row.packageName+"&date="+row.executorTime;
            window.location.href="/html2/system#/logManager"+params;
        };

        $scope.errLog = function(){
            window.location.href="/html2/system#/logManager";
        };
    }
});